package nl.weeaboo.sh.level;

import javax.media.opengl.GL;

import nl.weeaboo.game.gl.GLImage;
import nl.weeaboo.game.gl.GLImageStore;
import nl.weeaboo.sh.model.TileModel;
import nl.weeaboo.sh.model.TileSetModel;

public class TileSet {

	private TileSetModel model;
	private GLImage images[][];
	private GLImage editorImages[][];
	
	public TileSet(TileSetModel model, GLImageStore is, GLImage baseImage, GLImage editorImage) {
		this.model = model;
		
		/*baseImage.getTexture().setInterpolation(GL.GL_NEAREST);
		if (editorImage != null) {
			editorImage.getTexture().setInterpolation(GL.GL_NEAREST);
		}*/
		
		int tileSize = Level.tileSize;
		images = new GLImage[getWidth()][getHeight()];
		editorImages = new GLImage[getWidth()][getHeight()];
		for (int y = 0; y < getHeight(); y++) {
			for (int x = 0; x < getWidth(); x++) {
				String imageId = String.format("_ts_%s_%d_%d", baseImage.getId(), x, y);
				images[x][y] = baseImage.derive(is, imageId, x * tileSize, y * tileSize, tileSize, tileSize);
				if (editorImage != null) {
					String editorId = String.format("_ts_%s_%d_%d", editorImage.getId(), x, y);
					editorImages[x][y] = editorImage.derive(is, editorId, x * tileSize, y * tileSize, tileSize, tileSize);
				}
				
				images[x][y].getTexture().setInterpolation(GL.GL_NEAREST);
			}
		}
	}
	
	//Functions
	
	//Getters
	public int getWidth() { return model.getWidth(); }
	public int getHeight() { return model.getHeight(); }
	
	public TileModel getTileModel(int x, int y) { return model.getTileModel(x, y); }
	public GLImage getImage(int x, int y) { return images[x][y]; }
	public GLImage getEditorImage(int x, int y) { return editorImages[x][y]; }
	
	public String getId() {
		return model.getId();
	}

	//Setters
	
}
